package io.renren.modules.yckj.controller;


import cn.hutool.core.date.DateTime;
import io.renren.common.utils.Result;
import io.renren.modules.yckj.common.FileConfig;
import io.renren.modules.yckj.common.UploadUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录接口
 *
 * @author Mark lidong2010@gmail.com
 */
@RestController
@RequestMapping("/yckj/common")
@Tag(name = "通用接口")
public class CommonController {

    @Autowired
    private FileConfig fileConfig;

    @Operation(summary = "上传文件接口")
    @PostMapping("/uploadFile")
    @ResponseBody
    public Result<Object> uploadFile(@RequestParam("file") MultipartFile file, String type) {
        String filePath = fileConfig.getFilePath() + "/" + type;
        String fileUrl = fileConfig.getFileUrl() + "/" + type;
//        boolean reName = true;
//        String fileName = "";
//        if (StringUtils.isNotBlank(type) && type.equals("project")) {
//            String datePath = "/" + DateTime.now().toString("yyyyMMddHHmmss");
//            filePath = filePath + datePath;
//            fileUrl = fileUrl + datePath;
//            reName = false;
//        } else {
//            reName = true;
//        }
        String fileName = UploadUtil.upload(file, filePath, true);
        String fileAllPath = fileUrl + "/" + fileName;
        Map<String, String> map = new HashMap<>();
        map.put("name", fileName);
        map.put("url", fileAllPath);
        return new Result<>().ok(map);
    }

//    @Operation(summary = "上传文件接口")
//    @PostMapping("/uploadFile3d")
//    @ResponseBody
//    public Result<Object> uploadFile3d(@RequestParam("file") MultipartFile file, String id) {
//        String filePath = fileConfig.getFilePath() + "/project/" + id;
//        String fileUrl = fileConfig.getFileUrl() + "/project/" + id;
//        String fileName = UploadUtil.upload(file, filePath, false);
//        String fileAllPath = fileUrl + "/" + fileName;
//        Map<String, String> map = new HashMap<>();
//        map.put("name", fileName);
//        map.put("url", fileAllPath);
//        return new Result<>().ok(map);
//    }
}


